package evolutionaryballoons;

import static evolutionaryballoons.Configuration.*;

import java.awt.geom.Point2D;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

public class Points {
	private List<Point2D> points;
	
	public Points() {
		points = new LinkedList<Point2D>();
		
		Random random = new Random();
		
		for (int i = 0; i < sNumberOfPoints; i++) {
			points.add(new Point2D.Double(
					sPointsMargin + random.nextDouble() * (sWidth - 2 * sPointsMargin), 
					sPointsMargin + random.nextDouble() * (sHeight - 2 * sPointsMargin)));
		}
	}
	
	public Points(Points parent) {
		points = new LinkedList<Point2D>();
		points.addAll(parent.points);
	}
	
	public List<Point2D> getAll() {
		return new LinkedList<Point2D>(points);
	}
	
	public Point2D getClosestPoint(double x, double y) {
		if (points.size() == 0) {
			return null;
		}
		
		Point2D closestPoint = points.get(0);
		
		for (Point2D p: points) {
			double d = p.distance(x, y);
			
			if (d < closestPoint.distance(x, y)) {
				closestPoint = p;
			}
		}
		
		return closestPoint;
	}
	
	public Point2D get(int index) {
		try {
			return points.get(index);
		} catch (IndexOutOfBoundsException e) {
			return null;
		}
	}

	public void remove(Point2D p) {
		points.remove(p);
	}

	public int getCollected() {
		return sNumberOfPoints - points.size();
	}

	public int indexOf(Point2D p) {
		return points.indexOf(p);
	}
}
